package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.domain.Category;
import com.itheima.domain.Dish;
import com.itheima.domain.Setmeal;
import com.itheima.excecption.CustomException;
import com.itheima.mapper.CatgoryMapper;
import com.itheima.service.CatgoryService;
import com.itheima.service.DishService;
import com.itheima.service.SetMealService;
import com.itheima.tool.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CatgoryServiceImpl extends ServiceImpl<CatgoryMapper, Category>
implements CatgoryService{

    @Autowired
    DishService dService;

    @Autowired
    SetMealService SService;


    @Override
    public void remove(Long id) {
        LambdaQueryWrapper<Dish> dlqw = new LambdaQueryWrapper<>();
        dlqw.eq(Dish::getCategoryId,id);
        int Dcount = dService.count(dlqw);
        if (Dcount>0){
            throw new CustomException("分类中关联了其他菜品,无法删除");
        }
        LambdaQueryWrapper<Setmeal> Slqw = new LambdaQueryWrapper<>();
        Slqw.eq(Setmeal::getCategoryId,id);
        int Scount = SService.count(Slqw);
        if (Scount >0){
            throw new CustomException("分类中关联了其他套餐,无法删除");
        }
        super.removeById(id);
    }
}
